{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "autoscroll": "json-false",
    "collapsed": false,
    "ein.tags": [
     "worksheet-0"
    ]
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys \n",
    "from os import getcwd, path\n",
    "sys.path.append(path.dirname(getcwd()))\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sb\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "autoscroll": "json-false",
    "collapsed": false,
    "ein.tags": [
     "worksheet-0"
    ]
   },
   "outputs": [],
   "source": [
    "from utils import data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from utils.paper import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def str_formatter(s):\n",
    "    return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{{{number_all_patients:29}}}\n"
     ]
    }
   ],
   "source": [
    "hyper_label_printer(str_formatter, label=\"number_all_patients\",\n",
    "                    s=len(data.init_cohort(print_provenance=False,\n",
    "                                           only_patients_with_bams=False,\n",
    "                                           exclude_patient_ids=set([])).as_dataframe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{{{number_wes_patients:25}}}\n"
     ]
    }
   ],
   "source": [
    "hyper_label_printer(str_formatter, label=\"number_wes_patients\",\n",
    "                    s=len(data.init_cohort(print_provenance=False).as_dataframe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "autoscroll": "json-false",
    "collapsed": false,
    "ein.tags": [
     "worksheet-0"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{{{number_rnaseq_patients:26}}}\n"
     ]
    }
   ],
   "source": [
    "hyper_label_printer(str_formatter, label=\"number_rnaseq_patients\",\n",
    "                    s=len(data.init_cohort(print_provenance=False,\n",
    "                                           exclude_patient_ids=set([])).as_dataframe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inner join with tcr_tumor: 29 to 24 rows\n",
      "inner join with tcr_tumor: 29 to 24 rows\n",
      "{{{number_tcr_tumor_patients:24}}}\n"
     ]
    }
   ],
   "source": [
    "hyper_label_printer(str_formatter, label=\"number_tcr_tumor_patients\",\n",
    "                    s=len(data.init_cohort(join_with=\"tcr_tumor\",\n",
    "                                           print_provenance=False,\n",
    "                                           only_patients_with_bams=False,\n",
    "                                           exclude_patient_ids=set([])).as_dataframe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inner join with tcr_peripheral_a: 29 to 29 rows\n",
      "inner join with tcr_peripheral_a: 29 to 29 rows\n",
      "{{{number_tcr_a_patients:29}}}\n"
     ]
    }
   ],
   "source": [
    "hyper_label_printer(str_formatter, label=\"number_tcr_a_patients\", \n",
    "                    s=len(data.init_cohort(join_with=\"tcr_peripheral_a\",\n",
    "                     only_patients_with_bams=False,\n",
    "                     exclude_patient_ids=set([]),\n",
    "                     print_provenance=False).as_dataframe()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "inner join with tcr: 26 to 107 rows\n",
      "inner join with tcr: 26 to 107 rows\n",
      "{{{number_tcr_a_post_patients:24}}}\n"
     ]
    }
   ],
   "source": [
    "cohort_tcr = data.init_cohort(join_with=\"tcr\",\n",
    "                     exclude_patient_ids=set([]),\n",
    "                     print_provenance=False)\n",
    "df_tcr = cohort_tcr.as_dataframe()\n",
    "df_tcr = df_tcr[[\"patient_id\", \"Time Point\", \"Sample Type\"]]\n",
    "df_tcr_blood = df_tcr[df_tcr[\"Sample Type\"] == \"PBMC\"]\n",
    "df_tcr_grouped = df_tcr_blood.groupby(\"patient_id\").agg(lambda rows: list(rows))\n",
    "df_tcr_patients_with_pre_post_blood = df_tcr_grouped[df_tcr_grouped[\"Time Point\"].apply(\n",
    "    lambda l: \"A\" in l and len(l) > 1)]\n",
    "hyper_label_printer(str_formatter, label=\"number_tcr_a_post_patients\", s=len(df_tcr_patients_with_pre_post_blood))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  },
  "name": "TCR Clonality.ipynb"
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
